package cn.iocoder.yudao.module.scrm.dal.mysql.marketing;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.category.MarketingCategoryPageReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.marketing.MarketingCategoryDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 营销分类 Mapper
 *
 * @author admin
 */
@Mapper
public interface MarketingCategoryMapper extends BaseMapperX<MarketingCategoryDO> {

    default PageResult<MarketingCategoryDO> selectPage(MarketingCategoryPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MarketingCategoryDO>()
                .likeIfPresent(MarketingCategoryDO::getDeptName, reqVO.getDeptName())
                .eqIfPresent(MarketingCategoryDO::getLevel, reqVO.getLevel())
                .likeIfPresent(MarketingCategoryDO::getName, reqVO.getName())
                .eqIfPresent(MarketingCategoryDO::getMarketingType, reqVO.getMarketingType())
                .betweenIfPresent(MarketingCategoryDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(MarketingCategoryDO::getIfLevel, reqVO.getIfLevel())
                .eqIfPresent(MarketingCategoryDO::getParentId, reqVO.getParentId())
                // 使用 FIND_IN_SET 函数进行精确匹配
                .apply(reqVO.getDeptId() != null && !reqVO.getDeptId().isEmpty(),
                        "FIND_IN_SET({0}, dept_id) > 0", reqVO.getDeptId()) // 检查 deptId 是否在 dept_id 中
                .orderByDesc(MarketingCategoryDO::getId));
    }

}